package com.geekchique.runningbuddy.activity;


import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.Menu.Item;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.controller.DayOfJogDetailsControllerImpl;
import com.geekchique.runningbuddy.controller.IDayOfJogDetailsController;
import com.geekchique.runningbuddy.controller.IOptionsMenu;
import com.geekchique.runningbuddy.controller.OptionsMenuImpl;
import com.geekchique.runningbuddy.model.Jog;
import com.geekchique.runningbuddy.view.dayofjogdetails.DayOfJogDetailsListAdapter;


public class Today extends ListActivity {

	private IDayOfJogDetailsController dayOfJogDetailsController;
	private IOptionsMenu optionsMenu;

	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.today);
		optionsMenu = new OptionsMenuImpl(this);
        
        dayOfJogDetailsController = new DayOfJogDetailsControllerImpl(this);
        
        listLastJogDetailsAndTotals();
        
        listAlljogsRunToday(dayOfJogDetailsController.getTodaysJogs());
    }

	private void listLastJogDetailsAndTotals() {
		
		TextView miles = new TextView(this);
		TextView name = new TextView(this);
		TextView timerun = new TextView(this);

		miles.setText("");
		name.setText("");
		timerun.setText("");
		
		dayOfJogDetailsController.createTodayView( 
				this,
				(Button) findViewById(R.id.goto_journal),
				miles,
				name,
				timerun);
        dayOfJogDetailsController.populateTodaysJoggingTotals();
	}

	private void listAlljogsRunToday(List<Jog> jogEntries) {
		
		if (jogEntries.size() > 0){
			displayAllJogsTodayDetails(jogEntries);
		}
		
	}

	private void displayAllJogsTodayDetails(List<Jog> jogEntries) {
//		long longOfTimeRun = entry.getStopTime() - entry.getStartTime();
		
        ListAdapter adapter = new DayOfJogDetailsListAdapter(this, jogEntries);
		
        setListAdapter(adapter);
	
	}
	
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(Item item) {
		optionsMenu.selectItem(item);
	    return super.onOptionsItemSelected(item);
	}


}
